/*
 * @Author: lei
 * @Date: 2021-06-12 15:02:02
 * @LastEditTime: 2021-06-12 16:52:52
 * @LastEditors: Please set LastEditors
 * @Description: vector的基本使用：类模板/成员函数：构造析构等/容量相关/修改器/迭代器/元素访问
 * @FilePath: /temp/vector.cpp
 */
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

//4中基本的构造方式
vector<int> vec{1,3,5,7,9};//初始化列表构造
vector<int> vec2(vec.begin(),vec.end()-2);//一对迭代器构造
vector<int> vec3(5);//初始化5个元素，每个元素默认初始化
vector<int> vec4(5,10);//初始化5个元素，每个元素值为10
vector<string> vec5{"fdsf","hjvk","wyeuiy",""};
vector<string> vec6(5,"lei");
vector<string> vec7;//默认构造

//遍历方式选择：通过占位参数，整数表示遍历方式一，字符表示遍历方式二
/**
 * @description: 遍历输出 任意类型vector容器中的元素
 * @param {vector<T>}
 * @return {void}
 * 遍历方式一：operator[]返回所需元素的 引用
 */
template<typename T>
void Traverse(vector<T> &tar,int k) 
{
    std::cout<<"---------通过下标遍历---------------"<<std::endl;
    for(int i=0;i<tar.size();i++)
    {
        std::cout<<"第 "<<i+1<<" 个元素："<<tar[i]<<std::endl;
    }
    return ;
}

/**
 * @description: 遍历输出 任意类型vector容器中的元素
 * @param {vector<T>}
 * @return {void}
 * 遍历方式二： 通过 迭代器（可以理解为指向vector元素的指针）遍历
 */
template<typename T>
void Traverse(vector<T> &tar,char k)
{
    //vector<T>::iterator it=tar.begin();错误了，要加typename
    typename vector<T>::iterator it=tar.begin();
    std::cout<<"----------通过迭代器遍历--------------"<<std::endl;
    for(;it!=tar.end();it++)
    {
        std::cout<<"第 "<<it-tar.begin()<<" 个元素："<<*it<<std::endl;
    }
    return ;
}
void Print()
{
    Traverse(vec,1);
    Traverse(vec2,2);
    Traverse(vec3,3);
    Traverse(vec4,4);

    Traverse(vec5,1);
    vec7.push_back("fjkdls");
    vec7.push_back("jfsdhk");
    Traverse(vec7,'c');
}

//各类迭代器的使用?
void Iter()
{
    Traverse(vec,'y');
    vector<int>::iterator it=vec.begin();
    it=vec.end();
    vector<int>::const_iterator it2=vec.cbegin();
    it2=vec.cend();
    vector<int>::reverse_iterator it3=vec.rbegin();
    it3=vec.rend();
    vector<int>::const_reverse_iterator it4=vec.crbegin();
    it4=vec.crend();
}

template<typename T>
void Size_Capacity(vector<T> &tar)
{
    std::cout<<"empty? :"<<boolalpha<<tar.empty()<<std::endl;
    std::cout<<"max_size :"<< tar.max_size()<<std::endl;
    std::cout<<"size :"<< tar.size()<<std::endl;
    std::cout<<"capacity :"<< tar.capacity()<<std::endl;
    vec.push_back(0);
    std::cout<<"after push_back 0:"<<std::endl;
    std::cout<<"size :"<< tar.size()<<std::endl;
    std::cout<<"capacity :"<< tar.capacity()<<std::endl;
    vec.shrink_to_fit();
    std::cout<<"size :"<< tar.size()<<std::endl;
    std::cout<<"capacity :"<< tar.capacity()<<std::endl;
    vec.resize(1);
    std::cout<<"size :"<< tar.size()<<std::endl;
    std::cout<<"capacity :"<< tar.capacity()<<std::endl;
    vec.reserve(1);
    std::cout<<"size :"<< tar.size()<<std::endl;
    std::cout<<"capacity :"<< tar.capacity()<<std::endl;
}

template<typename T>
void Modify(vector<T> &tar)
{
    Traverse(tar,1);
    //tar.clear();
    tar.emplace(tar.begin()+2,9);
    cout<<"after clear"<<endl;
    Traverse(tar,4);
}
int main()
{
    //Print();
    //Iter();
    //Size_Capacity(vec);
    Modify(vec);
    return 0;
}